package com.xf.protocol;

import com.xf.common.Invocation;
import com.xf.common.Serviceinfo;
import com.xf.rigister.RemoteRigister;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.Vector;

public class HttpServerHandler_con_rig implements Runnable{
    private ServerSocket serverSocket;
    public HttpServerHandler_con_rig(ServerSocket serverSocket) {
        this.serverSocket=serverSocket;
    }
    public void run() {
        //System.out.println("New con connected");
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("接收客户端请求时发生异常！");
        }
        ObjectInputStream inputStream=null;
        try {
            inputStream = new ObjectInputStream(clientSocket.getInputStream());
            Object o = inputStream.readObject();
            if(o instanceof Invocation){
                handle_reqforcall((Invocation) o,clientSocket);
            }
            else {
                handle_reqforinfo(clientSocket);
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("接收客户端请求时发生异常！");
        }
    }

    private void handle_reqforcall(Invocation invocation,Socket clientSocket) {
        Object result=null;
        ObjectOutputStream objectOutputStream=null;
        System.out.println("收到客户端对服务的请求。");
        String string = invocation.getInterfaceName() + invocation.getVersion();
        if(!string.startsWith("com.xf")) string="com.xf."+string;
        if(!RemoteRigister.getSelectinfo().containsKey(string)){
            System.out.println("找不到该服务: "+string);
            result="没有找到该服务: "+string;
        }
        else if(!RemoteRigister.getSelectinfo().get(string).containsKey(invocation.getMethod())){
            System.out.println("服务接口中没有此方法: "+invocation.getMethod());
            result="服务接口中没有此方法: "+invocation.getMethod();
        }
        else {
            Vector<String> vector = RemoteRigister.getSelectinfo().get(string).get(invocation.getMethod());
            int size = vector.size();
            Random rand = new Random();
            int randomInt = rand.nextInt(size);
            String s= vector.get(randomInt);
            String[] split = s.split(":");
            String address =null;
            Integer port =null;
            if(split.length>2){//ipv6地址特殊处理
                address=split[0];
                for(int i=1;i<split.length-1;i++) address+=":"+split[i];
                port=Integer.parseInt(split[split.length-1]);
            }
            else {//ipv4地址
                address = split[0];
                port = Integer.parseInt(split[1]);
            }
            HttpClient httpClient = new HttpClient();
            result = httpClient.cfs_rig_pro(address, port, invocation);
        }
        try {
            objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            objectOutputStream.writeObject(result);
            objectOutputStream.close();
            clientSocket.close();
            System.out.println("返回结果成功！");
        } catch (IOException e) {
            System.out.println("返回结果时异常！");
        }
    }

    private void handle_reqforinfo(Socket clientSocket){
        System.out.println("收到客户端对服务列表的请求。");
        ObjectOutputStream objectOutputStream = null;
        Serviceinfo serviceinfo = new Serviceinfo(serverSocket.getInetAddress().getHostAddress(),
                serverSocket.getLocalPort(),RemoteRigister.getSelectinfo());
        try {
            objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            objectOutputStream.writeObject(serviceinfo);
            objectOutputStream.close();
            clientSocket.close();
        } catch (IOException e) {
            System.out.println("返回服务列表时发生异常！");
        }
        System.out.println("服务信息发送完成！");
    }
}
